from ..attributes import *

from .alignment import AlignedObject


class GUIElement(AlignedObject):
    dialog   = Attribute()
    screen   = dialog._.screen
    
    renderer   = Attribute(initiable = False)
    size       = renderer._.size
    width      = renderer._.width
    height     = renderer._.height
    raw_size   = renderer._.raw_size
    raw_width  = renderer._.raw_width
    raw_height = renderer._.raw_height
    
    is_first_update = Attribute(initial = True, initiable = False, writable = True)
    
    @property
    def is_ready(self):
        return self.screen.is_initialized
    
    @property
    def needs_update(self):
        return True
    
    def render_with(self, primitive, *args, **kwds):
        self.renderer = primitive(model = self, *args, **kwds)
    
    def changed(self):
        self.renderer.update()

    def update(self):
        if self.needs_update or self.is_first_update:
            self.on_update()
            self.changed()
            
            self.is_first_update = False
    
    def resize(self):
        self.on_resize()
    
    def aligned(self):
        self.on_move()
        self.renderer.on_move()
    
    def on_resize(self):
        pass
    
    def on_update(self):
        pass
    
    def on_move(self):
        pass


class GUIElementPrimitive(Object):
    model  = Attribute()
    screen = model._.screen
    
    texture   = Attribute(initiable = False, changed = lambda self: self.resize)
    object    = Attribute(initiable = False)
    
    @property
    def size(self):
        return (None, None)
    
    @property
    def width(self):
        return self.size[0]
    
    @property
    def height(self):
        return self.size[1]
    
    @property
    def raw_size(self):
        return self.size
    
    @property
    def raw_width(self):
        return self.raw_size[0]
    
    @property
    def raw_height(self):
        return self.raw_size[1]

    def resize(self):
        self.model.resize()
        self.on_resize()
    
    def on_resize(self):
        pass
    
    def aligned(self):
        self.on_move()
    
    def on_move(self):
        pass
